self.addEventListener('message', async (event) => {
  const file = event.data
  const fileName =await getFileName(file)
  self.postMessage(fileName)
  /**
   * @description: 根据file对象，计算文件hash，并补上ext，作为文件名
   * @param {*} file
   * @return {*}
   */  
  async function getFileName(file) {
    const fileHash = await computeFileHash(file)
    const fileExtension = file.name.split(".").pop()
    return `${fileHash}.${fileExtension}`
  }
  /**
   * @description: 根据file对象，计算其SHA-256加密之后的ArrayBuffer
   * @param {*} file
   * @return {*}
   */  
  async function computeFileHash(file) {
    const arrayBuffer = await file.arrayBuffer()
    const hashBuffer = await crypto.subtle.digest("SHA-256", arrayBuffer)
    return hashBuffer2Hex(hashBuffer)
  }
  /**
   * @description: 根据SHA-256加密之后的ArrayBuffer，转换为TypedArray，计算为16进制的字符串
   * @param {*} hashBuffer
   * @return {*}
   */  
  function hashBuffer2Hex(hashBuffer) {
    const hashArray = Array.from(new Uint8Array(hashBuffer))
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("")
    return hashHex
  }
})